var Game = function()
{
	this.ctx;
	this.tm;
	this.entities = new Array();
	this.frameCount = 0;
	this.ticksPassed = 0;
	this.timeLast = 0;
	
	this.addEntity = function(entity)
	{
		console.log("Entity added");
		entity.setGameObject(this);
		this.entities.push(entity);
	}
	
	this.setContext = function(ctx)
	{
		this.ctx = ctx;
	}
	
	this.run = function()
	{
		this.clear();
		this.calculate();
		this.draw();
		
	}
	
	this.clear = function()
	{
		this.ctx.fillStyle = color_canvasBg;
		this.ctx.rect(0, 0, width, height);  
		this.ctx.closePath();
		this.ctx.fill();
	}
	
	this.calculate = function()
	{
		date = new Date();
		timeNow = date.getSeconds() + (date.getMilliseconds()/10);

		if(!this.timeLast)
		{
			this.timeLast = timeNow;
		}
		
		this.ticksPassed =  timeNow - this.timeLast;
		this.timeLast = timeNow;
		
		for(i = 0; i < this.ticksPassed; i++)
		{
			this.entities.foreach(function(k, entity)
			{
				entity.calculate();
			});
		}
		
	}
	
	this.draw = function()
	{
		this.entities.foreach(function(k, entity)
		{
			entity.draw(this.ctx);
		});
		
		this.ctx.fillStyle = "#000000";
		this.ctx.fillText("TicksPassed: "+parseInt(this.ticksPassed), this.ctx.canvas.width-400, this.ctx.canvas.height-25);
		this.ctx.fillText("EntityCount: "+this.entities.length, this.ctx.canvas.width-300, this.ctx.canvas.height-25);
		this.ctx.fillText("FPS: "+this.frameCount, this.ctx.canvas.width-100, this.ctx.canvas.height-25);
		this.ctx.fillText("FrameCount: "+this.frameCount, this.ctx.canvas.width-100, this.ctx.canvas.height-25);
		this.frameCount++;
	}
	
	this.click = function(position)
	{
		this.entities.foreach(function(k, entity)
		{
			entity.setTarget(position);
		});
	}
}
